package com.link2room.soundwave.facade.impl;

import com.link2room.common.service.ICommonService;
import com.link2room.common.util.DateUtil;
import com.link2room.soundwave.constant.SoundwaveConstant;
import com.link2room.soundwave.dto.param.base.PmsRateParamDto;
import com.link2room.soundwave.entity.PmsTaskBase;
import com.link2room.soundwave.facade.IPmsRateHandlerFacadeService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Optional;

/**
 * Created by VegetaKo on 2016/11/15.
 */
public abstract class AbstractPmsRateHandlerFacadeServiceImpl implements IPmsRateHandlerFacadeService {

    abstract void syncPMSRate(PmsRateParamDto pmsRateParamDto) throws Exception;

    abstract void syncPMSHotelRate(PmsRateParamDto pmsRateParamDto) throws Exception;

    abstract void syncPMSRateIncrement(PmsRateParamDto pmsRateParamDto) throws Exception;

    abstract void syncPMSRateHotelIncrement(PmsRateParamDto pmsRateParamDto) throws Exception;

    @Resource
    private ICommonService commonService;

    @Override
    public void syncPMSRateHandler(PmsRateParamDto pmsRateParamDto) throws Exception {
        Long dayStep = Optional.ofNullable(pmsRateParamDto.getDayStep()).orElse(7L);
        Date start=DateUtil.parseDateNoTime(DateUtil.addDays(new Date(),pmsRateParamDto.getStartIndex()));
        Date end=DateUtil.addDays(start,dayStep);
        pmsRateParamDto.setDayStep(dayStep);
        pmsRateParamDto.setStartDate(start);
        pmsRateParamDto.setEndDate(end);
        syncPMSRate(pmsRateParamDto);
    }

    @Override
    public void syncPMSHotelRateHandler(PmsRateParamDto pmsRateParamDto) throws Exception{
        syncPMSHotelRate(pmsRateParamDto);
    }

    @Override
    public void syncPMSRateIncrementHandler(PmsRateParamDto pmsRateParamDto) throws Exception {
        PmsTaskBase queryTaskBase=new PmsTaskBase();
        queryTaskBase.setLhotelGroupCode(pmsRateParamDto.getLhotelGroupCode());
        queryTaskBase.setTaskType(SoundwaveConstant.TASK_TYPE.PRICE);
        PmsTaskBase pmsTaskBase=commonService.findOneEQ(PmsTaskBase.class,queryTaskBase);
        PmsTaskBase pmsTaskBase1=Optional.ofNullable(pmsTaskBase).orElseGet(()->{
            PmsTaskBase pmsTaskBaseNew=new PmsTaskBase();
            pmsTaskBaseNew.setLhotelGroupCode(pmsRateParamDto.getLhotelGroupCode());
            pmsTaskBaseNew.setTaskType(SoundwaveConstant.TASK_TYPE.PRICE);
            pmsTaskBaseNew.setLastUpdatetime(new Date());
            return pmsTaskBaseNew;
        });
        pmsRateParamDto.setLastModifyDate(DateUtil.getNewFormatDateString(pmsTaskBase1.getLastUpdatetime()));
        pmsTaskBase1.setLastUpdatetime(new Date());
        commonService.saveOrUpdate(pmsTaskBase1);
        syncPMSRateIncrement(pmsRateParamDto);
    }

    @Override
    public void syncPMSRateHotelIncrementHandler(PmsRateParamDto pmsRateParamDto) throws Exception {
        syncPMSRateHotelIncrement(pmsRateParamDto);
    }
}
